define(['amazeui', 'jquery', 'global/url', 'global/constants'],
 function(amazeui, jquery, url, constants) {
        return ['$scope', '$rootScope', 'UrlService', 'UtilService', 'ActiveCodeService', 'AdminService', 
        function($scope, $rootScope, UrlService, UtilService, ActiveCodeService, AdminService) {
        	var accessToken = $scope.accessToken, filter = {
		        	'pagesize': 10,
		        	'pno': 1,
		        	'search': ''
		        }, params = {
		        	'accessToken': accessToken,
		        	'filter': filter
		        }

	  	

		    $scope.searchActiveCodes = function (event) {
		        params.filter.search = $scope.search;
		        params.filter.pagesize = $scope.activeCodes.pagesize;
		        _getActiveCodeList(params);
		    } 

		    $scope.forwardPage = function (event) {
		        var pno = event.target.text;
		        params.filter.search = $scope.search;
		        params.filter.pagesize = $scope.activeCodes.pagesize;
		        params.filter.pno = pno;
		        _getActiveCodeList(params);
		    }

		    $scope.prePage = function () {
		        var currentPage = $scope.activeCodes.currentPage;
		        if (currentPage === 1) {
		        	return;
		        }
		        params.filter.search = $scope.search;
		        params.filter.pagesize = $scope.activeCodes.pagesize;
		        params.filter.pno = currentPage - 1;
		        _getActiveCodeList(params);
		    }

		    $scope.nextPage = function () {
		        var currentPage = $scope.activeCodes.currentPage;
		        if (currentPage === $scope.activeCodes.totalPagers) {
		        	return;
		        }
		        params.filter.search = $scope.search;
		        params.filter.pagesize = $scope.activeCodes.pagesize;
		        params.filter.pno = currentPage + 1;
		        _getActiveCodeList(params);
		    }

			function _getActiveCodeList (params) {
			    ActiveCodeService.listActiveCodes(params).success(function(response, status) {
			  		if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
			  		    $scope.activeCodes = response.jsonResponse.response.data;
			  	    } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
			  	        AdminService.Admin.logout();
			  	        window.location.href = constants.pages.login;
			  	    }
		        });
			}

		UtilService.renderSelect();
	    _getActiveCodeList(params);
        }];
	}
)
